DROP PROCEDURE [dbo].[pr0060]
GO
CREATE  PROCEDURE [dbo].[pr0060]
	@TransactionID varchar(20), 
	@DivisionID varchar(20),  
	@RDVoucherID varchar(20), 
	@TranYear  int, @TranMonth int,  
	@RDVoucherDate Datetime, 	
	@RDVoucherNo varchar(20),	
	@EmployeeID varchar(20), 	@ObjectID varchar(20),  --	@VoucherTypeID varchar(20), 
	@CreateUserID varchar(20), 	@CreateDate  Datetime,	@Description varchar(225),
	@WareHouseID varchar(20), 	@WareHouseID2 varchar(20), 	
	@Status int, 			@KindVoucherID int,
	@CurrencyID varchar(20), 	@ExchangeRate money,	
	@InventoryID varchar(20), 	@UnitID varchar(20),  		@MethodID tinyint,
	@ActualQuantity Decimal(28,8), 
	@UnitPrice Decimal(28,8),
	@ConvertedQuantity Decimal(28,8),
	@ConvertedUnitPrice Decimal(28,8),	
	@OriginalAmount Decimal(28,8), 
	@ConvertedAmount Decimal(28,8),
	@DebitAccountID varchar(20),
	@CreditAccountID varchar(20),   
	@SourceNo varchar(20),
	@IsLimitDate tinyint, 
	@IsSource tinyint,
	@LimitDate as datetime,
	@TableID as varchar(20),
	@BatchID as varchar(20),
	@Ana01ID as varchar(20),	                                      
	@Ana02ID as varchar(20),	                                      
	@Ana03ID as varchar(20),
	@Notes as varchar(250)	,
	@VoucherTypeID varchar(20),
	 @PeriodID  varchar(20), 
	@ProductID  varchar(20),
	@IsTemp tinyint,
	@OrderID   varchar(20)

	
 AS


If (@IsSource<>0 or @IsLimitDate <>0 or @MethodID in (1,2,3,5,7) ) and    @KindVoucherID in (1,3,5,7,9)
	Begin
		--print 'Insert vao bang tinh gia xuat kho: tb0007'	
		Insert tb0007 (InventoryID, DivisionID, WareHouseID, ReVoucherID, ReTransactionID,   ReVoucherNo,
	                                	ReVoucherDate,  ReTranMonth,ReTranYear, ReSourceNo,    ReQuantity,  DeQuantity,
		                 	EndQuantity,    UnitPrice
				  ,  Status, LimitDate)
		Values (@InventoryID, @DivisionID, @WareHouseID, @RDVoucherID, @TransactionID,   @RDVoucherNo, 
			@RDVoucherDate,  @TranMonth, @TranYear, @SourceNo, @ConvertedQuantity, 0, 
			@ConvertedQuantity, 
			Case when @ConvertedQuantity <>0 then  @ConvertedAmount/@ConvertedQuantity else  @ConvertedUnitPrice End,
			0, @LimitDate)
	End



If @KindVoucherID =3 --- la xuat van chuyen noi bo
	Begin
		Exec pr0053 @DivisionID, @WareHouseID, @TranMonth, @TranYear, @InventoryID, @ConvertedAmount, @ConvertedQuantity, @DebitAccountID, @CreditAccountID, 11 		-- (1) la cap nhat tang,(0 ) la cap nhat giam
		Exec pr0053 @DivisionID, @WareHouseID2, @TranMonth, @TranYear, @InventoryID, @ConvertedAmount, @ConvertedQuantity, @DebitAccountID, @CreditAccountID, 10 		-- (1) la cap nhat tang,(0 ) la cap nhat giam		
	End
Else
	If @KindVoucherID in (1,5,7,9)  --- Nhap kho, dieu chinh tang : 1: nhap kho - 5: mua hang nhap kho - 7: hang ban tra lai nhap kho
		Exec pr0053 @DivisionID, @WareHouseID, @TranMonth, @TranYear, @InventoryID, @ConvertedAmount, @ConvertedQuantity, @DebitAccountID, @CreditAccountID, 1 		-- (1) la cap nhat tang,(0 ) la cap nhat giam
	Else	
		If @KindVoucherID in (2,4,6,8)  	--- Xuat kho, dieu chinh giam: 2: Xuat kho - 4: ban hang xuat kho
		Exec pr0053 @DivisionID, @WareHouseID, @TranMonth, @TranYear, @InventoryID, @ConvertedAmount, @ConvertedQuantity, @DebitAccountID, @CreditAccountID, 0 		-- (1) la cap nhat tang,(0 ) la cap nhat giam




IF @TableID in ('tb0104', 'tb3010')   and    @IsTemp =0 and  ( @KindVoucherID<>3 or ( @KindVoucherID= 3 and @DebitAccountID <> @CreditAccountID) )
	Insert tb0191
		(VoucherID,  BatchID,     TransactionID,        TableID,
            		 DivisionID,   TranMonth,   TranYear,    TransactionTypeID,
		 CurrencyID,          CurrencyIDCN, ObjectID,   DebitAccountID,   CreditAccountID, 
		 ExchangeRate,          OriginalAmount,                 ConvertedAmount,
		 ExchangeRateCN,          OriginalAmountCN,                 
             		 VoucherDate,              VoucherTypeID,                               VoucherNo            ,                        

		 Orders ,EmployeeID ,   Ana01ID, Ana02ID, Ana03ID,
             		 VDescription ,  BDescription , TDescription,    Quantity,   UnitPrice,
		InventoryID,          UnitID,               Status,        
		ProductID, PeriodID, OrderID,
		 CreateDate ,                 CreateUserID ,        LastModifyDate  ,            LastModifyUserID )    
	Values
		(@RDVoucherID, isnull(@BatchID,''), @TransactionID, @TableID ,
		@DivisionID, @TranMonth, @TranYear, 
		Case When @KindVoucherID in (1,3,5,7,9) then 'T05' else
			Case When @KindVoucherID in ( 2,4,6,8) then 'T06' else'T99' end End,
		@CurrencyID,        @CurrencyID, @ObjectID,   @DebitAccountID, @CreditAccountID,
		@ExchangeRate,          @OriginalAmount,                @ConvertedAmount,
		@ExchangeRate,          @OriginalAmount,
             		 @RDVoucherDate,              @VoucherTypeID,                               @RDVoucherNo            ,                        
		 1 , @EmployeeID , @Ana01ID, @Ana02ID, @Ana03ID,        
	               @Description ,  @Description , @Notes ,   @ActualQuantity,      @UnitPrice ,                
		 @InventoryID,          @UnitID,      0,
		 @ProductID, @PeriodID, @OrderID,
		 @CreateDate ,                 @CreateUserID ,        @CreateDate  ,            @CreateUserID)


GO